package com.skd.mall.seckill.controller;


import com.alibaba.fastjson.JSON;
import com.skd.common.utils.R;
import com.skd.mall.seckill.dto.SeckillSkuRedisDto;
import com.skd.mall.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @RequestMapping("/currentSeckillSessionSkus")
    @ResponseBody
    public R getCurrentSeckillSessionSkus(){
        List<SeckillSkuRedisDto> currentSeckillSkus = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", JSON.toJSONString(currentSeckillSkus));
    }

    @RequestMapping("/getSeckillSessionBySkuId/{skuId}")
    @ResponseBody
    public R getSeckillSessionBySkuId(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisDto dto = seckillService.getSeckillSessionBySkuId(skuId);
        return R.ok().put("data",JSON.toJSONString(dto));
    }

    /**
     * http://seckill.skd.com/seckill/kill?killId=2_15&code=fbc7759350ef4d1497012c5d538a4287&num=1
     * @return
     */
    @RequestMapping("seckill/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("code") String code,
                          @RequestParam("num") Integer num,
                          Model model){
        //1校验是否登录，拦截器
        //2
        String orderSN = seckillService.kill(killId,code,num);
        model.addAttribute("orderSn",orderSN);
        return "success";
    }

}
